package com.BMS.DAOImpl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.ParameterMode;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.procedure.ProcedureCall;
import org.springframework.stereotype.Repository;

import com.BMS.DAO.GiveBackDao;
import com.BMS.entity.GiveBack;

/**
 * 图书归还实体类DAO层实现类
 * @author lenovo
 *
 */
@Repository("GiveBackDaoImpl")
public class GiveBackDaoImpl implements GiveBackDao {

	@Resource
	private SessionFactory sessionFactory;
	
	private Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	@Override
	public void addGiveBack(GiveBack giveBack) {
		ProcedureCall procedure = this.getSession().createStoredProcedureCall("returnBook");
		procedure.registerParameter(0, String.class, ParameterMode.IN).bindValue(giveBack.getReader_ID());
		procedure.registerParameter(1, String.class, ParameterMode.IN).bindValue(giveBack.getManager_ID());
		procedure.registerParameter(2, String.class, ParameterMode.IN).bindValue(giveBack.getBook_ID());
		procedure.registerParameter(3, Date.class, ParameterMode.IN).bindValue(giveBack.getGiveBack_date());
		
		procedure.execute();
	}

	@Override
	public void deleteGiveBackById(int giveBack_id) {
		this.getSession().createQuery("delete GiveBack where giveBack_ID=?").setParameter(0, giveBack_id).executeUpdate();
	}

	@Override
	public GiveBack findGiveBackById(int giveBack_id) {
		return (GiveBack)this.getSession().createQuery("from GiveBack where giveBack_ID=?").setParameter(0, giveBack_id).uniqueResult();
	}

	@Override
	public void updateGiveBack(GiveBack giveBack) {
		this.getSession().update(giveBack);
	}

}
